package com.sduept.nwld.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DBConnection {
	public static final String url="jdbc:mysql://10.140.9.105:3306/nwld_bh_db_cim_new?useUnicode=true&characterEncoding=utf8&user=root&password=Sduept6636";
	protected Connection conn = null;
	public void getConnection(){
		 try {
			 conn = DriverManager.getConnection(url);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void closeConnection(){
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public List<?> query(String sql,int size){
		List<Object> list = new ArrayList<>();
		Statement st  = null;
		getConnection();
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				Object[] obj = new Object[size];
				for (int i = 0; i < obj.length; i++) {
					obj[i] = rs.getObject(i+1);
				}
				list.add(obj);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally{
			closeConnection();
		}
		return list;
	}
}
